package com.futao.springboot.learn.api.jmeter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author futao
 * @since 2022/11/7
 */
@RestController
@RequestMapping("in-series")
public class InSeriesController {
    private static final JSONArray DATAS = new JSONArray();

    static {
        for (int i = 0; i < 20; i++) {
            DATAS.add(new JSONObject().fluentPut("id", i).fluentPut("username", "用户-" + i).fluentPut("age", i + 20));
        }
    }

    @GetMapping("/users")
    public JSONObject list() {
        return new JSONObject().fluentPut("code", 0).fluentPut("data", DATAS);
    }


    @GetMapping("/user/{id}")
    public Object detail(@PathVariable("id") Integer userId) {
        for (Object data : DATAS) {
            if (Objects.equals(((JSONObject) data).get("id"), userId)) {
                return data;
            }
        }
        return new JSONObject().fluentPut("msg", "未查询到此用户");
    }
}

